package cn.ht.jywrwater.module.ht.service.customers;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.ht.jywrwater.module.ht.controller.admin.customers.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.customers.CustomersDO;
import cn.ht.jywrwater.framework.common.pojo.PageResult;

import cn.ht.jywrwater.module.ht.convert.customers.CustomersConvert;
import cn.ht.jywrwater.module.ht.dal.mysql.customers.CustomersMapper;

import static cn.ht.jywrwater.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ht.jywrwater.module.ht.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 用户 Service 实现类
 *
 * @author hzx
 */
@Service
@Validated
public class CustomersServiceImpl implements CustomersService {

    @Resource
    private CustomersMapper customersMapper;

    @Override
    public Long createCustomers(CustomersCreateReqVO createReqVO) {
        // 插入
        CustomersDO customers = CustomersConvert.INSTANCE.convert(createReqVO);
        customersMapper.insert(customers);
        // 返回
        return customers.getId();
    }

    @Override
    public void updateCustomers(CustomersUpdateReqVO updateReqVO) {
        // 校验存在
        validateCustomersExists(updateReqVO.getId());
        // 更新
        CustomersDO updateObj = CustomersConvert.INSTANCE.convert(updateReqVO);
        customersMapper.updateById(updateObj);
    }

    @Override
    public void deleteCustomers(Long id) {
        // 校验存在
        validateCustomersExists(id);
        // 删除
        customersMapper.deleteById(id);
    }

    private void validateCustomersExists(Long id) {
        if (customersMapper.selectById(id) == null) {
            throw exception(CUSTOMERS_NOT_EXISTS);
        }
    }

    @Override
    public CustomersDO getCustomers(Long id) {
        return customersMapper.selectById(id);
    }

    @Override
    public List<CustomersDO> getCustomersList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return customersMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CustomersDO> getCustomersPage(CustomersPageReqVO pageReqVO) {
        return customersMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CustomersDO> getCustomersList(CustomersExportReqVO exportReqVO) {
        return customersMapper.selectList(exportReqVO);
    }

}
